package demo.socket;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpSend {

	public static void main(String[] args) throws IOException {
		DatagramSocket ds = new DatagramSocket(8888);
		Frame f = new Frame("��������");
		TextArea ta = new TextArea(10,50);
		ta.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					try {
						send(ta, ds);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
			}
		});
		f.setLayout(new FlowLayout());
		f.setBounds(900, 500, 400, 300);
		f.add(ta);
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				ds.close();
				System.exit(0);
			}
		});
		f.setVisible(true);
	}

	public static void send(TextArea ta, DatagramSocket ds) throws SocketException, IOException, UnknownHostException {
		byte[] data = ta.getText().getBytes();
		ta.setText("");
		ds.send(new DatagramPacket(data, data.length, InetAddress.getByName("127.0.0.1"), 10000));
	}

}
